// information/upLoadLicense/index.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyUid: 0,
    license: [],//营业执照
    showLicense: [],//渲染营业执照
    idCardPosi: [],//身份证正面
    showIdCardPosi: [],
    idCrdRe: [],//身份证背面
    showIdCrdRe: [],//
    otherPhoto: [],//其他证件
    showOtherPhoto: [],//
    bankId: '',
    pic: 1,//1营业执照2身份证正面3身份证背面4其他证件
    isEditor: false,
    isSelf: true,
    discount: '',//签约玖玖卡比例
    brokerageDiscount: '',//商家佣金比例
    remark: '',
    openFuli: false,//是否开通福鲤卡
    discountFuli: '',
    remarkFuli: '',
    applymentMsg: '',
    industry: '请选择行业',
    range: ['餐饮', '食品生鲜', '零售批发', '生活娱乐', '其他'],
    citys: [],
    cityList: [],
    provinces: [],
    selProvince: '请选择省',
    selCity: '请选择市',
    selProvinceIndex: 0,
    selCityIndex: 0,
    branchBankList: [],
    branchBank: '请选择开户支行',
    bankName: '请选择开户银行',
    bankList: [],
    userCount: 100
  },

  openFuli: function () {
    this.setData({
      openFuli: !this.data.openFuli
    })
  },

  toSign: function () {
    let that = this;
    if (that.data.license.length == 0 || that.data.idCardPosi.length == 0 || that.data.idCrdRe.length == 0) {
      utils.showToast('请上传营业执照/身份证（正反面');
      setTimeout(() => {
        wx.reLaunch({
          url: '/information/paySet/index?seltoid=' + that.data.applyUid,
        })
      }, 2000);
         return;
    }
    if (!that.data.discount) {
      utils.showToast('请输入商家签约折扣比例');
      return;
    }
    if (that.data.discount > 100) {
      utils.showToast('请输入小于100的折扣比例');
      return;
    }
    if (that.data.openFuli) {
      if (!that.data.discountFuli) {
        utils.showToast('请输入福鲤卡用户支付比例');
        return;
      }
      if (that.data.discountFuli > 100) {
        utils.showToast('请输入小于100的折扣比例');
        return;
      }
    } else {
      that.data.discountFuli = 0;
    }
    if (that.data.brokerageDiscount == '') {
      that.data.brokerageDiscount = 0;
    }
    if(that.data.brokerageDiscount + that.data.discount>100){
      utils.showModal('商家签约折扣+平台佣金折扣不能大于100');
      return;
    }
    wx.navigateTo({
      url: '../signature/index?applyUid=' + that.data.applyUid + '&isEditor=' + that.data.isEditor + '&discount=' + that.data.discount + '&remark=' + that.data.remark + '&discountFuli=' + that.data.discountFuli + '&remarkFuli=' + that.data.remarkFuli + '&brokerageDiscount=' + that.data.brokerageDiscount
    })
    // wx.navigateTo({
    //   url: '../signature/index?license=' + that.data.license + '&idcard=' + that.data.idCardPosi + '&idcardre=' + that.data.idCrdRe + '&otherphoto=' + that.data.otherPhoto + '&banknumber=' + that.data.bankId + '&bankname=' + that.data.branchBank + '&applyUid=' + that.data.applyUid + '&isEditor=' + that.data.isEditor + '&discount=' + that.data.discount + '&remark=' + that.data.remark + '&discountFuli=' + that.data.discountFuli + '&remarkFuli=' + that.data.remarkFuli + '&industry=' + that.data.industry + '&city=' + that.data.selCity + '&brokerageDiscount=' + that.data.brokerageDiscount
    // })
  },

  toPreStep: function () {
    wx.navigateBack({
      delta: 1
    })
  },



  

  bindDiscountBrokerage: function (e) {
    let discount = this.data.discount == '' ? 0 : this.data.discount;//签约玖玖卡比例
    let brokerageDiscount = e.detail.value=='' ? 0 : parseInt(e.detail.value);   //商家佣金比例
    let userCount = discount + brokerageDiscount;
    this.setData({
      brokerageDiscount: brokerageDiscount,
      userCount: userCount
    })
  },
  bindDiscount: function (e) {
    let discount= e.detail.value=='' ? 0 : parseInt(e.detail.value);   //签约玖玖卡比例    
    let brokerageDiscount = this.data.brokerageDiscount == '' ? 0 : this.data.brokerageDiscount;//商家佣金比例
    let userCount = discount + brokerageDiscount;
    this.setData({
      discount: discount,
      userCount: userCount
    })
  },
  bindRemark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  bindDiscountFuli: function (e) {
    this.setData({
      discountFuli: e.detail.value,
      openFuli: true
    })
  },
  bindRemarkFuli: function (e) {
    this.setData({
      remarkFuli: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.seltoid) {
      this.data.applyUid = options.seltoid;
      this.getJiujiuContract(options.seltoid);
    }
  },

  getJiujiuContract: function (uid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardApply?uid=' + uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let idCardPosi = [res.data.Data.IdCard]
            let idCrdRe = [res.data.Data.IdCard]
            let license = [res.data.Data.License]
            let applymentMsg = '';
            if (res.data.Data.State == 0) {
              applymentMsg = '审核中';
            } else if (res.data.Data.State == 1) {
              applymentMsg = '已审核';
            } else {
              applymentMsg = '已拒绝';
            }
            if (res.data.Data.ApplymentMsg) {
              applymentMsg += '：' + res.data.Data.ApplymentMsg
            }

            that.setData({
              discount: res.data.Data.ContractDiscount,
              discountFuli: res.data.Data.ContractDiscount2,
              brokerageDiscount: res.data.Data.BrokerageDiscount,              
              license: license,//营业执照
              idCardPosi: idCardPosi,//身份证正面
              idCrdRe: idCrdRe,//身份证背面
              remark: res.data.Data.Remark,
              applymentMsg: applymentMsg,
              isEditor: true,
              userCount: res.data.Data.ContractDiscount + res.data.Data.BrokerageDiscount
            })
          }
        }     
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (app.globalData.userInfo) {
      if (app.globalData.userInfo.Uid != that.data.applyUid) {
        that.setData({
          isSelf: false
        })
      } else {//自己
        that.data.openFuli = false;
      }
    } else {
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1500)
    }
  }
})